﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections;
using WebSilverlight.Web;
using WebSilverlight.Helpers;

namespace WebSilverlight.Views
{
    public partial class InvoiceWorkWith : Page
    {
	#region Properties	
     
	#endregion
	
        public InvoiceWorkWith()
        {            
            InitializeComponent();
            if(this.dcInvoiceContext1 == null)                
				this.dcInvoiceContext1 = new InvoiceDomainContext();            
        }    
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {            
        }                
        private void AddNew_Click(object sender, RoutedEventArgs e)
        {
			CallEdit(false, true);
        }
        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
             CallEdit(true, false);
        }
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
             CallEdit(false, false);
        }
        private void CallEdit(bool IsReadOnly, bool IsNew)
        {
			InvoiceWrapper current = null;
            bool error = false;
            IList list = dataGridInvoice1.SelectedItems;
            
            if (IsNew)
                 current = new InvoiceWrapper();
            else
            {
                 if (list.Count != 1)
                 {
                     System.Windows.MessageBox.Show("select one element");
                     error = true;
                 }
                 else
                     current = (InvoiceWrapper)list[0];
            }
            if (!error)
			{   
				 if (ddsInvoice1.HasChanges)
				 {                     
					 System.Windows.MessageBox.Show("Changes Discarded");                     
					 this.dataGridInvoice1.CancelEdit();
					 this.ddsInvoice1.RejectChanges();
				 }                

				 var editw = new InvoiceEditWindow(!IsReadOnly);				 
				 editw.DomainContext = this.dcInvoiceContext1;
				 editw.DDS = this.ddsInvoice1;
				 editw.Current = current;
				 editw.IsNew = IsNew;
				 if(IsNew)
                     editw.Closed += new EventHandler(editw_Closed);
				 editw.Show();
			 }       
        }

        void editw_Closed(object sender, EventArgs e)
        {   
			//Duplicate Entity Key W.A.
			this.dcInvoiceContext1 = new InvoiceDomainContext();            
            this.ddsInvoice1.Load();            
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
			if(this.dataGridInvoice1.SelectedItems.Count == 1)
            {
                InvoiceWrapper current = (InvoiceWrapper)this.dataGridInvoice1.SelectedItem;
                this.dcInvoiceContext1.InvoiceWrappers.Remove(current);
            }

        }
        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {        				 
			this.dataGridInvoice1.CommitEdit(DataGridEditingUnit.Row, true);
            this.ddsInvoice1.SubmitChanges();
            this.GridActionsBottom.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void CancelEditButton_Click(object sender, RoutedEventArgs e)
        {
			if (ddsInvoice1.HasChanges)
			{			
				 dataGridInvoice1.CancelEdit();
				 ddsInvoice1.RejectChanges();
			}
			this.GridActionsBottom.Visibility = System.Windows.Visibility.Collapsed;
        }
        private void dataGridInvoice1_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            this.GridActionsBottom.Visibility = System.Windows.Visibility.Visible;
        } 
	
		/// <summary>
		/// Prompt Call event. 		
		/// </summary> 
        private void ClienteIdColumn_Prompt(object sender, RoutedEventArgs e)
        {
			var prompt = new ClientePrompt();
            prompt.Closed += new EventHandler(ClienteIdColumn_Prompt_Closed);
            prompt.Show();
        }  
        /// <summary>
		/// Prompt CallBack. Sets the prompt return
		/// </summary>       
		void ClienteIdColumn_Prompt_Closed(object sender, EventArgs e)
        {
            var prompt = (ClientePrompt)sender;
            prompt.Closed -= this.ClienteIdColumn_Prompt_Closed;

            if (prompt.Selected != null)
            {				 
				 this.GridActionsBottom.Visibility = System.Windows.Visibility.Visible;	 
                 InvoiceWrapper Current = (InvoiceWrapper)this.dataGridInvoice1.SelectedItem;
                 Current.ClienteId = prompt.Selected.ClienteId;
                 Current.ClienteName = prompt.Selected.ClienteName;
			}
        }         
		 		
    }
}
